package cn.lingyangwl.agile.food.core.controller.app;

import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.agile.food.core.model.rqrs.cart.CartListReq;
import cn.lingyangwl.agile.food.core.model.rqrs.cart.CartResp;
import cn.lingyangwl.agile.food.core.model.rqrs.cart.CartSaveReq;
import cn.lingyangwl.agile.food.core.model.rqrs.cart.CartUpdateReq;
import cn.lingyangwl.agile.food.core.service.CartService;
import cn.lingyangwl.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 购物车
 * 
 * @author shenguangyang
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/app/cart")
public class AppCartController {
    @Resource
    private CartService cartService;

    @ApiOperation(value = "查找购物车列表")
    @GetMapping("/list")
    @RamResource(action = "appShop:cart:query", name = "app-查询购物车商品", type = ActionType.READ)
    public Resp<CartResp> listCart(@Validated CartListReq req) {
        return Resp.ok(cartService.listCart(req));
    }

    @ApiOperation(value = "保存购物车")
    @PostMapping("/save")
    @RamResource(action = "appShop:cart:save", name = "app-添加商品到购物车", type = ActionType.WRITE)
    public Resp<CartResp> saveCart(@Validated @RequestBody CartSaveReq req) {
        return Resp.ok(cartService.saveCart(req));
    }
  
    @ApiOperation(value = "更新购物车")
    @PostMapping("/changeNum")
    @RamResource(action = "appShop:cart:changeNum", name = "app-改变购物车中的商品数量", type = ActionType.WRITE)
    public Resp<CartResp> changeNum(@Validated @RequestBody CartUpdateReq req) {
        CartResp resp = cartService.changeNum(req);
        return Resp.ok(resp);
    }
  
    @ApiOperation(value = "批量删除购物车")
    @DeleteMapping("/delete")
    @RamResource(action = "appShop:cart:delete", name = "app-删除购物车中的商品", type = ActionType.WRITE)
    public Resp<?> batchDeleteCart(@RequestParam(value = "cartType") String cartType,
                                   @RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(cartService.batchDeleteCart(ids, cartType));
    }
}
